from myelin.Ns import MYELINS
from myelin.server.core.ft import ModelDriver

from nodetypes.ntypes.Document.view.MML import Document, Transform
from nodetypes.Ns import GRAPHS_NS

class Document(Document):
    
    newAgentDriver = staticmethod(ModelDriver.FromRepo)
            
    def createContent(self, context, node, locale):
        self.createAgentProfileContent(context, node, locale)        
        return super(Document, self).createContent(context, node, locale)
    
    def createAgentProfileContent(self, context, node, locale):
        model = context.model
        map = context.uriMapper.apply
        
        curNode = node.xml_append(
            node.xml_create_element(
                u'a:grouping', MYELINS,
                attributes={
                    u'key'      : u'account-details',
                }
            )
        )
        
        cxa = curNode.xml_append
        cxc = curNode.xml_create_element
        
        depiction = model.foafDepiction
        if depiction:
            depiction = depiction[0]
            cxa(
                cxc(
                    u'a:image', MYELINS,
                    attributes={
                        u'role'     : u"depiction",
                        u'uri'      : map(depiction.rdf_value),
                        u'modified' : depiction.rdf_join('dctermsModified'),
                    }
                )
            )
        else:
            cxa(
                cxc(
                    u'a:image', MYELINS,
                    attributes={
                        u'role'     : u"depiction",
                    }
                )
            )
        
        for agent in model.foafPrimaryTopic:
            self.createAgentDetails (
                context,
                cxa(
                    cxc(
                        u'a:dict', MYELINS,
                        attributes={
                            u'key'      : u'agent',
                        }
                    )
                ),
                agent
            )
            
        return node
    
    def createAgentDetails (self, context, node, agent):
        nxa = node.xml_append
        nxc = node.xml_create_element
        
        href = context.server.graphs.get(GRAPHS_NS + 'agents')
        href = href and href.get('public') or None
        if href:
            a_join = agent.rdf_clone(
                agent.Initialize(
                    self.newAgentDriver(
                        context.repo,
                        href
                    )
                )
            ).rdf_join
        else:
            a_join = agent.rdf_join
        
        nxa(
            nxc(
                u'a:entry', MYELINS,
                attributes = {
                    u'key'      : u'nick',
                },
                content = a_join('foafNick')
            )
        )
        nxa(
            nxc(
                u'a:entry', MYELINS,
                attributes = {
                    u'key'      : u'name',
                },
                content = a_join('foafName')
            )
        )
        value = a_join('foafMbox')
        if value:
            nxa(
                nxc(
                    u'a:entry', MYELINS,
                    attributes = {
                        u'key'      : u'mbox',
                    },
                    content = value,
                )
            )
        
        value = a_join('foafHomepage')
        if value:
            nxa(
                nxc(
                    u'a:entry', MYELINS,
                    attributes = {
                        u'key'      : u'homepage',
                    },
                    content = value,
                )
            )
            
        return node


class Transform(Transform):
    
    newDocument = Document